package cn.stylefeng.guns.modular.flowable.service.impl;

import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.common.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.flowable.entity.ActIdMembership;
import cn.stylefeng.guns.modular.flowable.mapper.ActIdMembershipMapper;
import cn.stylefeng.guns.modular.flowable.model.params.ActIdMembershipParam;
import cn.stylefeng.guns.modular.flowable.model.result.ActIdMembershipResult;
import cn.stylefeng.guns.modular.flowable.service.ActIdMembershipService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gblfy
 * @since 2019-12-20
 */
@Service
public class ActIdMembershipServiceImpl extends ServiceImpl<ActIdMembershipMapper, ActIdMembership> implements ActIdMembershipService {

    @Resource
    private ActIdMembershipMapper actIdMembershipMapper;

    /**
     * 根据用户id删除 用户和组中间数据
     *
     * @param userId
     */
    @Override
    public void deleteByUserId(String userId) {
        actIdMembershipMapper.deleteByUserId(userId);
    }

    @Override
    public void add(ActIdMembershipParam param){
        ActIdMembership entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ActIdMembershipParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ActIdMembershipParam param){
        ActIdMembership oldEntity = getOldEntity(param);
        ActIdMembership newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ActIdMembershipResult findBySpec(ActIdMembershipParam param){
        return null;
    }

    @Override
    public List<ActIdMembershipResult> findListBySpec(ActIdMembershipParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ActIdMembershipParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ActIdMembershipParam param){
        return param.getUserId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ActIdMembership getOldEntity(ActIdMembershipParam param) {
        return this.getById(getKey(param));
    }

    private ActIdMembership getEntity(ActIdMembershipParam param) {
        ActIdMembership entity = new ActIdMembership();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
